package it.polimi.digitalstate.webbeans;

import java.util.Date;

import it.polimi.digitalstate.dto.ElectionDTO;
import it.polimi.digitalstate.manager.AuthenticationManagerLocal;
import it.polimi.digitalstate.manager.ElectionManagerLocal;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import managerexception.CannotSaveElection;

@ManagedBean(name="electionFormBean")
@RequestScoped
public class ElectionFormBean {
	@EJB
	ElectionManagerLocal ejbelectionmanager;
	@EJB
	AuthenticationManagerLocal ejb_user;
	
	private ElectionDTO election = new ElectionDTO();
	
	public String getName() {
		return election.getName();
	}
	public void setName(String name) {
		election.setName(name);
	}
	
	public Date getCandidacystartdate() {
		if (election.getCandidacystartdate() == null) return null;
		else return new Date(election.getCandidacystartdate().getTime());
	}
	
	public void setCandidacystartdate(Date candidacystartdate) {
		if (candidacystartdate != null) election.setCandidacystartdate(new java.sql.Date(candidacystartdate.getTime()));
	}   
	
	public Date getCandidacyenddate() {
		if (election.getCandidacyenddate() == null) return null;
		else return new Date(election.getCandidacyenddate().getTime());	}

	public void setCandidacyenddate(Date candidacyenddate) {
		if (candidacyenddate != null) election.setCandidacyenddate(new java.sql.Date(candidacyenddate.getTime()));
	}   
	public Date getElectionstartdate() {
		if (election.getElectionstartdate() == null) return null;
		else return new Date(election.getElectionstartdate().getTime());
	}
	
	public void setElectionstartdate(Date Electionstartdate) {
		if (Electionstartdate != null) election.setElectionstartdate(new java.sql.Date(Electionstartdate.getTime()));
	}   
	
	public Date getElectionenddate() {
		if (election.getElectionenddate() == null) return null;
		else return new Date(election.getElectionenddate().getTime());	}

	public void setElectionenddate(Date Electionenddate) {
		if (Electionenddate != null) election.setElectionenddate(new java.sql.Date(Electionenddate.getTime()));
	}   

	public String  submitElection(){
		System.out.println("Election Details Decided");
		if (!(getCandidacystartdate() == null) && !(getCandidacyenddate() == null) &&
				!(getElectionstartdate() == null) && !(getElectionenddate() == null)&& 
				!election.getCandidacyenddate().before(election.getCandidacystartdate()) &&
				(!election.getElectionenddate().before(election.getElectionstartdate()))/* &&
				(election.getElectionstartdate().after(election.getCandidacyenddate()))*/
			)
		{
			election.setGovernor(ejb_user.getLoggedUser());
			try {
				ejbelectionmanager.createNewElection(election);
			} catch (CannotSaveElection e) {
				return "electiondecisionerror.xhtml?faces-redirect=ture";
			}
			System.out.println("Election Details Decided");
			return "electionsaved?faces-redirect=true";		
		}
		else
			System.out.println("There is a problem with the entered dates.");
			return "electiondecisionerror.xhtml?faces-redirect=ture";
	}
}

